package com.example.howard.picview.Tab2Fragment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.howard.picview.R;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Howard on 2016/9/2.
 */
public class Tab2FragmentAdapter extends BaseAdapter{
    LayoutInflater inflater;
    List<Map<String, Object>> titledata = new ArrayList<>();
    Context context;
//    Callback callback;

    public Tab2FragmentAdapter(Context c, List<Map<String, Object>> list) {
        this.inflater = LayoutInflater.from(c);
        context = c;
        this.titledata=list;
//        this.callback=call;
    }

    public void setData(List<Map<String, Object>> list){
        this.titledata=list;
    }

    public int getCount() {
        return titledata.size();
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    public View getView(final int position, View convertView,
                        ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.fragment_tab2_item, null);
            viewHolder = new ViewHolder();
            viewHolder.title = (ImageView) convertView
                    .findViewById(R.id.imagetitle);
            viewHolder.num= (TextView) convertView.findViewById(R.id.num);
            viewHolder.pathstr = (TextView) convertView
                    .findViewById(R.id.pathstr);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
//        viewHolder.title.setText(searchSongList.get(position).getSongname());
        Picasso
                .with(context)
                .load(new File(titledata.get(position).get("title").toString()))
                .resize(200,200)
                .centerCrop()
//                .placeholder(R.drawable.picassopic)
//                .error(R.drawable.picassopic)
                .into(viewHolder.title);

        viewHolder.num.setText("共"+titledata.get(position).get("num").toString()+"张");
        viewHolder.pathstr.setText(titledata.get(position).get("path").toString());


        return convertView;
    }

    class ViewHolder {
        ImageView title;
        TextView num;
        TextView pathstr;
    }

//    public interface Callback{
//        public void imageCallback (String path);
//    }
}
